//FileObj.cpp
//
#include "StdAfx.h"
#include "FileObj.h"
#include <sys/types.h>
#include <sys/stat.h>

FileObj::FileObj(){	
	size_ = 0;
	file_ = 0;

	close();
}

FileObj::~FileObj(){
	close();
}

bool FileObj::file_remove( char* name ){
	IS_NULL_RETURN( name, false );

	if( access( name, 0 ) != 0 )
		return false;
	
	if( remove( name ) < 0 )
		return false;
	return true;
}

bool FileObj::isExist( char* name ){
	if ( name == NULL ) return false;

	FILE* file = fopen( name, "r" );
	if(! file )
		return false;
	return true;	
}

bool FileObj::open(char * name, char * option){
	IS_NULL_RETURN( name, false );
	IS_NULL_RETURN( strlen( name ), false );
	IS_NULL_RETURN( option, false );	
    
	(void)checkValidName(name);
	fileName_ = name;

	file_ = fopen(fileName_.c_str(), option);
	if(! file_)
		return false;
	return true;	
}

void FileObj::close(){	
	size_ = 0;
	fileName_ = "";	

	if(! file_ )
		return;

    fflush(file_);
	fclose(file_);

	file_ = 0;
}

void FileObj::checkValidName(char* name){
	char replace_char = '_';
	int size = (int)strlen(name);
	for( int i=0 ; i < size ; i++ ){		
		if( isspace(name[i]) ){
			name[i] = replace_char;
		}
		if( name[i] == ':' ){
#ifndef WIN32
			name[i] = replace_char;
#else
			if(! ((i + 1) >= size) ){
#ifdef WIN32
				if( name[ (i + 1) ] == '\\' )
#else
				if( name[ (i + 1) ] == '/' )
#endif
					continue;
				else
					name[i] = replace_char;
			}
			else
				name[i] = replace_char;
#endif
		}
	}	
}

int FileObj::getLine(char* buf){
	int getSize=0;
	int ch=0;

	ch=getc(file_);
	if( ch == -1 ) 
		return 0;

	while(ch == '\n')
		ch=getc(file_);

	while( (ch != '\n') && ( ch != -1 ) ){
		buf[getSize++]=(char)ch;
		ch=getc(file_);
	}

	buf[getSize]='\0';

	return getSize;
}

int FileObj::getLine( char* buf, int max_char ){
	int loop = 0;
	int getSize=0;
	int ch=0;

	ch=getc(file_);
	if( ch == -1 ) 
		return 0;

	while(ch == '\n')
		ch=getc(file_);

	while(ch == 13)
		ch=getc(file_);

	while( (ch != '\n') && ( ch != -1 ) && (ch != 13) ){
		buf[getSize++] = (char)ch;
		ch = getc(file_);
		loop++;		
	}

	buf[getSize]='\0';
	return getSize;
}

__int64 FileObj::getMemory(char * buf, __int64 size){
	return fread(buf, sizeof(char), (size_t)size, file_);
}

__int64 FileObj::putMemory(char* buf, __int64 size){
	return fwrite(buf, 1, (size_t)size, file_);	
}

__int64 FileObj::getSize(char * fileName){
	if(size_) 
		return size_;
	
	char* fileName_tmp = fileName;
	if(! fileName_tmp )
		fileName_tmp = (char*)fileName_.c_str();

	if( fileName_.empty() )
		return 0;

    FILE * fp = fopen(fileName_tmp, "rb");
    if(! fp)
		return -1;

    fseek(fp, 0, SEEK_END);
    size_ = ftell(fp);
    fclose(fp);    

/*	
	struct __stat64 buf;
	int result = _stat64(fileName_tmp, &buf);
	if( result != 0 )
		return 0;

	size_ = buf.st_size;
*/	
	return size_;
}

char* FileObj::getExtension(){
	if ( fileName_.empty() ) return 0;

	int i = (int)fileName_.size();	

	while( i != 0 ){
		if( fileName_[i] == '.' ) 
			break;
		i--;		
	}

	IS_NULL_RETURN(i, false);

	i++;
	return (char*)&(fileName_[i]);	
}
